package com.house.keeper.controller;

import com.alibaba.fastjson.JSONObject;
import com.house.keeper.base.BaseController;
import com.house.keeper.base.Result;
import com.house.keeper.constant.ConstantConfig;
import com.house.keeper.entity.bean.UserBean;
import com.house.keeper.entity.vo.UserVo;
import com.house.keeper.service.DeptmentService;
import com.house.keeper.service.PositionService;
import com.house.keeper.service.RoleService;
import com.house.keeper.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户管理
 * @author zjj
 * @date 2022-05-12
 */
@Controller
@RequestMapping(value = "/user")
public class UserController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);
    @Autowired
    private UserService userService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private DeptmentService deptmentService;
    @Autowired
    private PositionService positionService;

    /**
     * 跳转到用户管理页面
     * @return
     */
    @GetMapping(value = "/view")
    @RequiresPermissions("system:user:view")
    public ModelAndView view(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("roles",roleService.selectAll());
        modelAndView.addObject("depts",deptmentService.selectAll());
        modelAndView.addObject("poss",positionService.selectAll());

        Map<Object,Object> map = new HashMap<>();
        map.put(ConstantConfig.STATUS_YES,"有效");
        map.put(ConstantConfig.STATUS_NO,"无效");

        modelAndView.addObject("states",map);

        modelAndView.setViewName("user/list");
        return modelAndView;
    }

    /**
     * 用户分页查询
     * @param userVo 查询条件
     * @return
     */
    @GetMapping(value = "/list")
    @RequiresPermissions("system:user:list")
    @ResponseBody
    public JSONObject list(UserVo userVo){
        Result result=userService.selectUserPage(userVo);
        logger.info(responseJSONObject(result).toString());
        return responseJSONObject(result);
    }

    /**
     * 更新用户状态
     * @param id
     * @return
     */
    @GetMapping(value = "/updateStatus")
    @RequiresPermissions("system:user:edit")
    @ResponseBody
    public JSONObject update(int id){
        Result result=userService.updateStatus(id);
        return responseJSONObject(result);
    }

    @GetMapping(value = "/update")
    @RequiresPermissions("system:user:edit")
    @ResponseBody
    public JSONObject edit(@RequestBody UserBean userBean){
        Result result = userService.update(userBean);
        return responseJSONObject(result);
    }


}
